/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FEMSolver;

import MathLib.triangulation.SaveStructures.Node;
import syntaxanalyser.PostfiksXY;

/**
 *
 * @author mark
 */
public class FEMQuadraticTRMatrixGenerator {

//i  a
    //j  b
    //m  c
    public static double kx;
    public static double ky;

    public static double Pex;
    public static double Pey;

    public static double[] GenerateF(Node A,Node B,Node C,Node D,Node E,Node F, String Func){
        double[] Fr = new double[6];

        try {
            Fr[0] = PostfiksXY.Calculate(Func, A.x, A.y);
            Fr[1] = PostfiksXY.Calculate(Func, B.x, B.y);
            Fr[2] = PostfiksXY.Calculate(Func, C.x, C.y);
            Fr[3] = PostfiksXY.Calculate(Func, D.x, D.y);
            Fr[4] = PostfiksXY.Calculate(Func, E.x, E.y);
            Fr[5] = PostfiksXY.Calculate(Func, F.x, F.y);

        } catch (Exception ex) {

            System.out.println("помилка в постфіксі ...");
        }
        double[][] Me = GenerateMe(A, C, E);
        return new  double[]{
            Me[0][0]*Fr[0]+Me[0][1]*Fr[1]+Me[0][2]*Fr[2]+Me[0][3]*Fr[3]+Me[0][4]*Fr[4]+Me[0][5]*Fr[5],
            Me[1][0]*Fr[0]+Me[1][1]*Fr[1]+Me[1][2]*Fr[2]+Me[1][3]*Fr[3]+Me[1][4]*Fr[4]+Me[1][5]*Fr[5],
            Me[2][0]*Fr[0]+Me[2][1]*Fr[1]+Me[2][2]*Fr[2]+Me[2][3]*Fr[3]+Me[2][4]*Fr[4]+Me[2][5]*Fr[5],
            Me[3][0]*Fr[0]+Me[3][1]*Fr[1]+Me[3][2]*Fr[2]+Me[3][3]*Fr[3]+Me[3][4]*Fr[4]+Me[3][5]*Fr[5],
            Me[4][0]*Fr[0]+Me[4][1]*Fr[1]+Me[3][2]*Fr[2]+Me[4][3]*Fr[3]+Me[4][4]*Fr[4]+Me[4][5]*Fr[5],
            Me[5][0]*Fr[0]+Me[5][1]*Fr[1]+Me[5][2]*Fr[2]+Me[5][3]*Fr[3]+Me[5][4]*Fr[4]+Me[5][5]*Fr[5],
        };
    }


    public static double[][] GenerateKe(Node A,Node B,Node C,Node D,Node E,Node F){
        double d = 1.0/(48.0*ComputeTriangleArea(A, B, C));

        /*double ai = C.x*E.y - E.x*C.y,
        aj = E.x*A.y - A.x*E.y,
        ak = A.x*C.y - C.x*A.y;*/
        double bi = C.y-E.y,
               bj = E.y-A.y,
               bk = A.y-C.y;
        double ci = E.x-C.x,
               cj = A.x-E.x,
               ck = C.x-A.x;

        return new double[][]{
            { kx*( 20*bi*bi ) + ky*( 20*ci*ci ),                                  kx*( 16*bi*bj ) +                                  ky*( 16*ci*cj ),   kx*( -4*bi*bj ) + ky*( -4*ci*cj ),                                                                                                   0, kx*( -4*bi*bk ) + ky*( -4*ci*ck ),                                  kx*( 16*bi*bk ) +                                  ky*( 16*ci*ck )  },
            { kx*( 16*bi*bj ) + ky*( 16*ci*cj ),                kx*( 32*(bi*bi + bi*bj + bj*bj) ) +                ky*( 32*(ci*ci + ci*cj + cj*cj) ),   kx*( 16*bi*bj ) + ky*( 16*ci*cj ), kx*( 16*bj*bj + 16*bi*bj + 32*bi*bk + 16*bj*bk ) + ky*( 16*cj*cj + 16*ci*cj + 32*ci*ck + 16*cj*ck ),                                 0, kx*( 16*bi*bi + 16*bi*bj + 16*bi*bk + 32*bj*bk ) + ky*( 16*ci*ci + 16*ci*cj + 16*ci*ck + 32*cj*ck )  },
            { kx*( -4*bi*bj ) + ky*( -4*ci*cj ),                                  kx*( 16*bi*bj ) +                                  ky*( 16*ci*cj ),   kx*( 20*bj*bj ) + ky*( 20*cj*cj ),                                  kx*( 16*bj*bk ) +                                  ky*( 16*cj*ck ), kx*( -4*bj*bk ) + ky*( -4*cj*ck ),                                                                                                   0  },
            {                                 0, kx*( 16*bj*bj + 16*bi*bj + 32*bi*bk + 16*bj*bk ) + ky*( 16*cj*cj + 16*ci*cj + 32*ci*ck + 16*cj*ck ),   kx*( 16*bi*bk ) + ky*( 16*ci*ck ),                kx*( 32*(bj*bj + bk*bj + bk*bk) ) +                ky*( 32*(cj*cj + ck*cj + ck*ck) ), kx*( 16*bj*bk ) + ky*( 16*cj*ck ), kx*( 16*bk*bk + 16*bk*bj + 32*bi*bj + 16*bi*bk ) + ky*( 16*ck*ck + 16*ck*cj + 32*ci*cj + 16*ci*ck )  },
            { kx*( -4*bi*bk ) + ky*( -4*ci*ck ),                                                                                                   0,   kx*( -4*bj*bk ) + ky*( -4*cj*ck ),                                  kx*( 16*bj*bk ) +                                  ky*( 16*cj*ck ), kx*( 20*bk*bk ) + ky*( 20*ck*ck ),                                  kx*( 16*bi*bk ) +                                  ky*( 16*ci*ck )  },
            { kx*( 16*bi*bk ) + ky*( 16*ci*ck ), kx*( 16*bi*bi + 16*bi*bj + 16*bi*bk + 32*bj*bk ) + ky*( 16*ci*ci + 16*ci*cj + 16*ci*ck + 32*cj*ck ),                                   0, kx*( 16*bk*bk + 16*bk*bj + 32*bi*bj + 16*bi*bk ) + ky*( 16*ck*ck + 16*ck*cj + 32*ci*cj + 16*ci*ck ), kx*( 16*bi*bk ) + ky*( 16*ci*ck ),                kx*( 32*(bi*bi + bk*bi + bk*bk) ) +                ky*( 32*(ci*ci + ck*ci + ck*ck) )  }
        };
    }

    public static double[][] GenerateMe(Node A,Node B,Node C){
        double d = (2*ComputeTriangleArea(A, B, C))/72.0;
        return new double[][]{
            { 12*d,    0, -2*d, -8*d, -2*d,    0},
            { 0,    64*d,    0, 32*d, -8*d, 32*d},
            { -2*d,    0, 12*d,    0, -2*d, -8*d},
            { -8*d, 32*d,    0, 64*d,    0, 32*d},
            { -2*d, -8*d, -2*d,    0, 12*d,    0},
            { 0,    32*d, -8*d, 32*d,    0, 64*d}
        };
    }

    public static double ComputeTriangleArea(Node A,Node B,Node C){

        double a =Math.sqrt( (A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y) );   //a-b
        double b =Math.sqrt( (C.x - B.x)*(C.x - B.x) + (C.y - B.y)*(C.y - B.y) );
        double c =Math.sqrt( (A.x - C.x)*(A.x - C.x) + (A.y - C.y)*(A.y - C.y) );
        double p = (a+b+c)/2.0;
        return Math.sqrt( p*(p-a)*(p-b)*(p-c) );
    }

    static double[][] GeneratePe(Node get, Node get0, Node get1) {
        return new double[6][6];
    }
}